//
//  EAMealSetViewController1.m
//  WhatToEat
//
//  Created by 方力 on 16/1/22.
//  Copyright © 2016年 Mac. All rights reserved.
//
#import "EARecommendMealCell.h"
#import "EAMealSetViewController1.h"
#import "EARoundMealController.h"

@implementation EAMealSetViewController1


-(void)viewDidLoad
{
    [super viewDidLoad];
    
    NSLog(@"%@\n%@\n%@",self.view,self.navigationController.navigationBar,self.tabBarController.tabBar)
    
    
    
    self.navigationItem.title=@"餐量设定";

    self.tableView=[[UITableView alloc] initWithFrame:self.view.bounds];
        [self.tableView registerNib:[UINib nibWithNibName:@"EARecommendMealCell" bundle:nil] forCellReuseIdentifier:@"EARecommendMealCell"];
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
    [self.view addSubview:self.tableView];
    
    self.tableView.height=self.view.height-self.navigationController.navigationBar.height-self.tabBarController.tabBar.height-20;
    
    
    NSLog(@"%@\n%@\n%@\n%@",self.view,self.navigationController.navigationBar,self.tabBarController.tabBar,self.tableView);
    
    self.tableView.separatorStyle=UITableViewCellSelectionStyleNone;
}

-(void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];
    
    self.navigationController.delegate=self;
    [self.tableView reloadData];


}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 80;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    EARecommendMealCell * cell = [tableView dequeueReusableCellWithIdentifier:@"EARecommendMealCell" forIndexPath:indexPath];
    
    NSDictionary * d=self.dataSouth[indexPath.row];
    
    
    
    cell.nameLabel.text=d[@"name"];
    
    
    
    [cell.headImageView setImageWithURL:[NSURL URLWithString:d[@"img"]]];
    
    

    cell.yuyangLabel.text=[NSString stringWithFormat:@"营养元素:%@",d[@"nutrition"]];
    cell.kllLabel.text=[NSString stringWithFormat:@"卡路里:%@",d[@"calory"]];
    cell.num=[NSString stringWithFormat:@"%@",d[@"num"]].intValue;
    
    
    cell.minusBlock=^(NSString * type){
        

        
        
        
        
        if ([type isEqualToString:@"remove"]) {
            [self.dataSouth removeObject:d];
            
            
            [self.tableView reloadData];
        }else
        {
        
            NSMutableDictionary * mutableDict=[d mutableCopy];
            
            [mutableDict setObject:type forKey:@"num"];
            
            [self.dataSouth replaceObjectAtIndex:indexPath.row withObject:mutableDict];
            
            
        
        
        }
        
        
        
        
        
        
        
    };
    
    
//    self.repairBtn.hidden=YES;
    
//    cell.foodsBtn.hidden=YES;
//    
//    cell.addBtn.hidden=YES;
    
    
    return cell;
    
    
}





- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    if ([viewController isKindOfClass:[EARoundMealController class]]) {
        
        
        
        self.hidesBottomBarWhenPushed=YES;
        
        
        
    }
    
    
    
    NSLog(@"%@",[viewController class]);
    
    
}





-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.dataSouth.count;
    
}

//
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    UIView * v=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kUI_SCREEN_WIDTH, 150)];
    
    v.backgroundColor=[UIColor whiteColor];
    
    UIView * linView=[[UIView alloc] init];
    
    
    linView.backgroundColor=kLineColors;
    [v addSubview:linView];
    
    [linView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(v).with.offset(2);
        
        make.size.mas_equalTo(CGSizeMake(kUI_SCREEN_WIDTH-25, 1));
        
        make.left.mas_equalTo(v).with.offset(10);
        
        
        
    }];
    
    EAMButton * bt1=[[EAMButton alloc] init];
    
    [bt1 setTitle:@"加入餐桌" forState:0];
    [v addSubview:bt1];
    self.addDiningtable=bt1;
    
    bt1.tag=1;
    [bt1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kUI_SCREEN_WIDTH/2, 30));
        
        make.top.mas_equalTo(v).with.offset(40);
        
        make.left.mas_equalTo(v).with.offset(kUI_SCREEN_WIDTH/4);
        
        
        
        
    }];
    
    
    EAMButton * bt2=[[EAMButton alloc] init];
    bt2.hidden=YES;
    
    [bt2 setTitle:@"推荐补全" forState:0];
    [v addSubview:bt2];
    self.repairBtn=bt2;
    bt2.tag=2;
    
    if (self.dataSouth.count<5) {
        bt2.backgroundColor=kTingColor;
        self.repairBtn.enabled=NO;
    }else{
        
        bt2.backgroundColor=[UIColor lightGrayColor];
        
        self.repairBtn.enabled=NO;
    }
    
    [bt2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kUI_SCREEN_WIDTH/3, 30));
        
        make.top.mas_equalTo(v).with.offset(40);
        
        make.right.mas_equalTo(v).with.offset(-30);
        
        
        
        
    }];
    
    
    
    [self.addDiningtable addTarget:self action:@selector(toMytableORrepear:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.repairBtn addTarget:self action:@selector(toMytableORrepear:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
    
    return v;
    
    
    
}

-(void)toMytableORrepear:(UIButton * )bt
{
    
    switch (bt.tag) {
        case 1:
        {
            
            
           
            ApplicationDelegate.isToRoot=YES;
            
            EATabBarController * tvc=(EATabBarController * )self.tabBarController;
            
            
            [tvc.customTabBarView selectItem:1];
            
            tvc.selectedIndex=1;
            
            
     
            
            
            [ApplicationDelegate sendMealWithMeals:self.dataSouth andMealType:self.mealtype andAty_level:self.aty_level];
            

            
          
        }
            break;
        case 2:
        {
            
            
            
            
            
        }
            break;
        default:
            break;
    }
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 150;
    
}
@end
